package cn.ibizlab.odoo.core.odoo_im_livechat.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel;
import cn.ibizlab.odoo.core.odoo_im_livechat.domain.Im_livechat_report_channel;
import cn.ibizlab.odoo.core.odoo_im_livechat.filter.Im_livechat_report_channelSearchContext;
import cn.ibizlab.odoo.core.odoo_im_livechat.service.IIm_livechat_report_channelService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[实时聊天支持频道报告] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Im_livechat_report_channelServiceImpl implements IIm_livechat_report_channelService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iim_livechat_report_channelClientService im_livechat_report_channelClientService;

    @Override
    public boolean create(Im_livechat_report_channel im_livechat_report_channel) {
        boolean bOk = false ;
        Iim_livechat_report_channel clientModel = this.convert2Model(im_livechat_report_channel,null);
        im_livechat_report_channelClientService.create(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_report_channel);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Im_livechat_report_channel> im_livechat_report_channels, int batchSize) {

        //im_livechat_report_channelClientService.updateBatch(im_livechat_report_channels,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //im_livechat_report_channelClientService.removeBatch(im_livechat_report_channels) ;

    }

    @Override
    public void createBatch(List<Im_livechat_report_channel> im_livechat_report_channels, int batchSize) {

                        //im_livechat_report_channelClientService.createBatch(im_livechat_report_channels,batchSize) ;

    }

    @Override
    public Im_livechat_report_channel get(Integer id) {
        Iim_livechat_report_channel clientModel = im_livechat_report_channelClientService.createModel();
        clientModel.setId(id);
        im_livechat_report_channelClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Im_livechat_report_channel get(Im_livechat_report_channel im_livechat_report_channel) {
        Im_livechat_report_channel retim_livechat_report_channel = this.get(im_livechat_report_channel.getId()) ;
    	BeanCopier copier=BeanCopier.create(retim_livechat_report_channel.getClass(), im_livechat_report_channel.getClass(), false);
        copier.copy(retim_livechat_report_channel, im_livechat_report_channel, null);
        return im_livechat_report_channel ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iim_livechat_report_channel clientModel = im_livechat_report_channelClientService.createModel();
        clientModel.setId(id);
        im_livechat_report_channelClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Im_livechat_report_channel im_livechat_report_channel) {
        boolean bOk = false ;
        Iim_livechat_report_channel clientModel = this.convert2Model(im_livechat_report_channel,null);
        im_livechat_report_channelClientService.update(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_report_channel) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Im_livechat_report_channel> searchDefault(Im_livechat_report_channelSearchContext context) {
        List<Im_livechat_report_channel> list = new ArrayList<Im_livechat_report_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel> clientModelList = im_livechat_report_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_report_channel>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据即时聊天查询
     */
    @Override
    public Page<Im_livechat_report_channel> selectByOdoo_livechat_channel(Integer id){
        Im_livechat_report_channelSearchContext context = new Im_livechat_report_channelSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_report_channel> list = new ArrayList<Im_livechat_report_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel> clientModelList = im_livechat_report_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_report_channel>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据讨论频道查询
     */
    @Override
    public Page<Im_livechat_report_channel> selectByOdoo_channel(Integer id){
        Im_livechat_report_channelSearchContext context = new Im_livechat_report_channelSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_report_channel> list = new ArrayList<Im_livechat_report_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel> clientModelList = im_livechat_report_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_report_channel>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Im_livechat_report_channel> selectByOdoo_partner(Integer id){
        Im_livechat_report_channelSearchContext context = new Im_livechat_report_channelSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_report_channel> list = new ArrayList<Im_livechat_report_channel>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel> clientModelList = im_livechat_report_channelClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_report_channel clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_report_channel>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iim_livechat_report_channel convert2Model(Im_livechat_report_channel domain , Iim_livechat_report_channel model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = im_livechat_report_channelClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDurationDirtyFlag())
            model.setDuration(domain.getDuration());
         if(domain.getStart_dateDirtyFlag())
            model.setStart_date(domain.getStart_date());
         if(domain.getStart_date_hourDirtyFlag())
            model.setStart_date_hour(domain.getStart_date_hour());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getChannel_nameDirtyFlag())
            model.setChannel_name(domain.getChannel_name());
         if(domain.getNbr_messageDirtyFlag())
            model.setNbr_message(domain.getNbr_message());
         if(domain.getUuidDirtyFlag())
            model.setUuid(domain.getUuid());
         if(domain.getTechnical_nameDirtyFlag())
            model.setTechnical_name(domain.getTechnical_name());
         if(domain.getNbr_speakerDirtyFlag())
            model.setNbr_speaker(domain.getNbr_speaker());
         if(domain.getLivechat_channel_id_textDirtyFlag())
            model.setLivechat_channel_id_text(domain.getLivechat_channel_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getChannel_id_textDirtyFlag())
            model.setChannel_id_text(domain.getChannel_id_text());
         if(domain.getChannel_idDirtyFlag())
            model.setChannel_id(domain.getChannel_id());
         if(domain.getLivechat_channel_idDirtyFlag())
            model.setLivechat_channel_id(domain.getLivechat_channel_id());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
        return model ;
    }

    public Im_livechat_report_channel convert2Domain( Iim_livechat_report_channel model ,Im_livechat_report_channel domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Im_livechat_report_channel();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDurationDirtyFlag())
            domain.setDuration(model.getDuration());
        if(model.getStart_dateDirtyFlag())
            domain.setStart_date(model.getStart_date());
        if(model.getStart_date_hourDirtyFlag())
            domain.setStart_date_hour(model.getStart_date_hour());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getChannel_nameDirtyFlag())
            domain.setChannel_name(model.getChannel_name());
        if(model.getNbr_messageDirtyFlag())
            domain.setNbr_message(model.getNbr_message());
        if(model.getUuidDirtyFlag())
            domain.setUuid(model.getUuid());
        if(model.getTechnical_nameDirtyFlag())
            domain.setTechnical_name(model.getTechnical_name());
        if(model.getNbr_speakerDirtyFlag())
            domain.setNbr_speaker(model.getNbr_speaker());
        if(model.getLivechat_channel_id_textDirtyFlag())
            domain.setLivechat_channel_id_text(model.getLivechat_channel_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getChannel_id_textDirtyFlag())
            domain.setChannel_id_text(model.getChannel_id_text());
        if(model.getChannel_idDirtyFlag())
            domain.setChannel_id(model.getChannel_id());
        if(model.getLivechat_channel_idDirtyFlag())
            domain.setLivechat_channel_id(model.getLivechat_channel_id());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        return domain ;
    }


}
